<template>
<div>
  <el-dialog
    :title="title"
    size="small"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="姓名" prop="name"
                :rules="[
                  {required: true, message:'姓名不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写姓名"     :disabled="true"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="学院" prop="institute"
                :rules="[
                  {required: true, message:'学院不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.institute" placeholder="请填写学院"     :disabled="true"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="年级" prop="grade"
                :rules="[
                  {required: true, message:'年级不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.grade" placeholder="请填写年级"     :disabled="true"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="班级" prop="classe"
                :rules="[
                  {required: true, message:'班级不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.classe" placeholder="请填写班级"     :disabled="true"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="学号" prop="studentId"
                :rules="[
                  {required: true, message:'学号不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.studentId" placeholder="请填写学号"     :disabled="true"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="当前辅导员" prop="currentInstructor"
                :rules="[
                  {required: true, message:'当前辅导员不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.currentInstructor" placeholder="请填写当前辅导员"     :disabled="true"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="目前德育分" prop="currentMoraleduPoint"
                :rules="[
                  {required: true, message:'目前德育分不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.currentMoraleduPoint" placeholder="请填写目前德育分"     :disabled="true"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="学籍状态" prop="state"
                :rules="[
                  {required: true, message:'学籍状态不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.state" placeholder="请填写学籍状态"     :disabled="true"></el-input>
           </el-form-item>
        </el-col>
<!--        <el-col :span="12">
            <el-form-item label="家长联系方式" prop="telphone"
                :rules="[
                  {required: true, message:'家长联系方式不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.telphone" placeholder="请填写家长联系方式"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="与学生关系" prop="relationship"
                :rules="[
                  {required: true, message:'与学生关系不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.relationship" placeholder="请填写与学生关系"     ></el-input>
           </el-form-item>
        </el-col> -->
    <el-col :span="24">
     <el-form-item label-width="0">
            <el-tabs v-model="studentMoreduMainTab">
            <el-tab-pane label="学生副表_德育加分">
                  <el-button size="small" @click="addStudentMoreduViceAddRow" type="primary">新增</el-button>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.studentMoreduViceAddList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                <el-table-column
                    prop="addReason"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="加分原因">
                  </el-table-column>
                <el-table-column
                    prop="addTime"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="加分时间">
                  </el-table-column>
                <el-table-column
                    prop="addPoint"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="加分数">
                  </el-table-column>
                <el-table-column
                    prop="addState"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="审核状态">
                    <template slot-scope="scope">
                        {{ $dictUtils.getDictLabel("audit_status", scope.row.addState, '-') }}
                    </template>
                  </el-table-column>
                  <el-table-column
                    prop="addEnclosure"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="附件">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewStudentMoreduViceAddRow(scope.row)" type="text" size="small">查看</el-button>
                      <el-button  @click="editStudentMoreduViceAddRow(scope.row)" type="text" size="small">编辑</el-button>
                      <el-button  @click="delStudentMoreduViceAddRow(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            <el-tab-pane label="学生副表-德育减分">
                  <el-button size="small" @click="addStudentMoreduViceSubRow" type="primary">新增</el-button>
                  <el-table
                  class="table"
                  size="small"
                  :data="inputForm.studentMoreduViceSubList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                <el-table-column
                    prop="subReason"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="扣分原因">
                  </el-table-column>
                <el-table-column
                    prop="subTime"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="扣分时间">
                  </el-table-column>
                <el-table-column
                    prop="subPoint"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="扣分数">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewStudentMoreduViceSubRow(scope.row)" type="text" size="small">查看</el-button>
                      <el-button  @click="editStudentMoreduViceSubRow(scope.row)" type="text" size="small">编辑</el-button>
                      <el-button  @click="delStudentMoreduViceSubRow(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
           </el-form-item>
          </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
  <StudentMoreduViceAddForm ref="studentMoreduViceAddForm" @addRow="saveStudentMoreduViceAddRow(arguments)"></StudentMoreduViceAddForm>
  <StudentMoreduViceSubForm ref="studentMoreduViceSubForm" @addRow="saveStudentMoreduViceSubRow(arguments)"></StudentMoreduViceSubForm>
</div>
</template>

<script>
  import StudentMoreduViceAddForm from './StudentMoreduViceAddForm'
  import StudentMoreduViceSubForm from './StudentMoreduViceSubForm'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        studentMoreduMainTab: '0',
        inputForm: {
          id: '',
          studentMoreduViceAddList: [],
          state: '',
          studentMoreduViceSubList: [],
          name: '',
          institute: '',
          grade: '',
          classe: '',
          studentId: '',
          currentInstructor: '',
          currentMoraleduPoint: '',
          relationship: '',
          telphone: ''
        }
      }
    },
    components: {
      StudentMoreduViceAddForm,
      StudentMoreduViceSubForm
    },
    methods: {
      init (method, id) {
        this.method = method
        if (method === 'add') {
          this.title = `新建学生德育档案`
        } else if (method === 'edit') {
          this.title = '修改学生德育档案'
        } else if (method === 'view') {
          this.title = '查看学生德育档案'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = id
          this.studentMoreduMainTab = '0'
          this.inputForm.studentMoreduViceAddList = []
          this.inputForm.studentMoreduViceSubList = []
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/moredumain/studentMoreduMain/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.studentMoreduMain)
              this.loading = false
            })
          }
        })
      },
      saveStudentMoreduViceAddRow (child) {
        if (child[0] === '') {
          this.inputForm.studentMoreduViceAddList.push(child[1])
        } else {
          this.inputForm.studentMoreduViceAddList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.studentMoreduViceAddList.splice(index, 1, child[1])
            }
          })
        }
      },
      addStudentMoreduViceAddRow (child) {
        this.$refs.studentMoreduViceAddForm.init('add')
      },
      viewStudentMoreduViceAddRow (child) {
        this.$refs.studentMoreduViceAddForm.init('view', child)
      },
      editStudentMoreduViceAddRow (child) {
        this.$refs.studentMoreduViceAddForm.init('edit', child)
      },
      delStudentMoreduViceAddRow (child) {
        this.inputForm.studentMoreduViceAddList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.studentMoreduViceAddList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.studentMoreduViceAddList.splice(index, 1, item)
          }
        })
      },
      saveStudentMoreduViceSubRow (child) {
        if (child[0] === '') {
          this.inputForm.studentMoreduViceSubList.push(child[1])
        } else {
          this.inputForm.studentMoreduViceSubList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.studentMoreduViceSubList.splice(index, 1, child[1])
            }
          })
        }
      },
      addStudentMoreduViceSubRow (child) {
        this.$refs.studentMoreduViceSubForm.init('add')
      },
      viewStudentMoreduViceSubRow (child) {
        this.$refs.studentMoreduViceSubForm.init('view', child)
      },
      editStudentMoreduViceSubRow (child) {
        this.$refs.studentMoreduViceSubForm.init('edit', child)
      },
      delStudentMoreduViceSubRow (child) {
        this.inputForm.studentMoreduViceSubList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.studentMoreduViceSubList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.studentMoreduViceSubList.splice(index, 1, item)
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/moredumain/studentMoreduMain/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
